//
//  LoginViewController.m
//  豆瓣
//
//  Created by liuxin on 15/10/26.
//  Copyright (c) 2015年 liuxin. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "LoginView.h"
#import "User.h"
#import "UserInfoDB.h"

@interface LoginViewController ()
@property (nonatomic, retain) UIAlertController *alert;
@property(nonatomic,retain)UIAlertController *alertFail;
@property(nonatomic,retain)LoginView *loginV;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"用户登录";
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.loginV = [[LoginView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    self.loginV.backgroundColor = [UIColor whiteColor];
    [self.loginV.Login addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
    [self.loginV.Register addTarget:self action:@selector(registerAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.loginV];
    [_loginV release];
    
    self.alert = [UIAlertController alertControllerWithTitle:@"消息" message:@"登陆成功" preferredStyle:UIAlertControllerStyleAlert];
    self.alertFail = [UIAlertController alertControllerWithTitle:@"消息" message:@"登陆失败,用户名或密码错误" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        //        block回调
        NSLog(@"取消");
    }];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action) {
        NSLog(@"ok");
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [self.alert addAction:cancle];
    [self.alert addAction:action];
    [self.alertFail addAction:cancle];
    [self.alertFail addAction:action];
}

-(void)loginAction{
    NSString*username = self.loginV.username.text;
    NSString*password = self.loginV.password.text;
    [[UserInfoDB shareDataBase]openDB];
    NSArray *array = [[UserInfoDB shareDataBase]selectUser:username];
    for (User *user in array) {
        if([user.password isEqualToString:password]){
            NSLog(@"yes");
            NSString*loginState = @"success";
            [[NSUserDefaults standardUserDefaults] setObject:loginState forKey:@"LoginState"];
            [[NSUserDefaults standardUserDefaults] setObject:user.userName forKey:@"LoginName"];
            [[NSUserDefaults standardUserDefaults] synchronize];
           [self presentViewController:self.alert animated:YES completion:^{
            }];
        }else{
            NSLog(@"no");
           [self presentViewController:self.alertFail animated:YES completion:^{
           }];
        }
    }
}

-(void)registerAction{
    RegisterViewController *registerVC = [[RegisterViewController alloc]init];
    [self.navigationController pushViewController:registerVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
